const Router = require('koa-router');
// const query = require('../lib/mysql');
const wrapper = require('../util/wrapper');
const checkError = require('../util/checkError');
const upload = require('../util/upload');

const router = new Router();

router.prefix('/api');

module.exports = router;

router.post('/upload/:qiniuImageStyleName', async (ctx, next) => {
  const { fields, files } = ctx.request.body;
  const { qiniuImageStyleName } = ctx.params;
  const { path } = files.file;
  const name = Date.now();
  const res = await upload(name, path).catch(err => console.log(err));
  if (fields && fields.id) {
    ctx.body = await wrapper({
      id: +fields.id,
      url: `http://cdn.zhongzhengtouzi.com/${res.key}!slider`,
    });
    return true;
  }
  ctx.body = await wrapper(`http://cdn.zhongzhengtouzi.com/${res.key}!${qiniuImageStyleName || 'content'}`);
});
